package com.shine.app.bean.responses;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.google.gson.annotations.SerializedName;
import com.shine.app.bean.BaseBean;
import com.shine.app.bean.GolfCourseBean;
import com.shine.app.bean.RateGroupBean;
import com.shine.app.commons.Commons;
import com.shine.app.commons.SortString;

public class FindTeeTimeResponse extends BaseBean {

	private static final long serialVersionUID = 1L;
	@SerializedName("HasError")
	private boolean hasError;
	@SerializedName("ErrorMessage")
	private String errorMessage;
	@SerializedName("ErrorType")
	private String errorType;
	@SerializedName("Data")
	private RateGroupBean[] data;

	public boolean isHasError() {
		return hasError;
	}

	public String getErrorType() {
		return errorType;
	}

	public void setErrorType(String errorType) {
		this.errorType = errorType;
	}

	public void setHasError(boolean hasError) {
		this.hasError = hasError;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public RateGroupBean[] getData() {
		return data;
	}

	public void setData(RateGroupBean[] data) {
		this.data = data;
	}

	public static Map<String, List<GolfCourseBean>> parse(
			FindTeeTimeResponse response) {
		Map<String, List<GolfCourseBean>> map = new HashMap<String, List<GolfCourseBean>>();
		try {
			if (response != null && !response.isHasError()) {
				RateGroupBean rateGroup[] = response.getData();
				List<GolfCourseBean> list = null;
				for (int i = 0, length = rateGroup.length; i < length; i++) {
					list = new ArrayList<GolfCourseBean>();
					RateGroupBean rateGroupBean = rateGroup[i];
					int rateGroupFrom = rateGroupBean.getRateGroupFrom();
					int rateGroupTo = rateGroupBean.getRateGroupTo();

					String mark = "$" + rateGroupFrom + " - $" + rateGroupTo;
					GolfCourseBean[] golfCourseBeans = rateGroupBean
							.getCourses();
					for (int j = 0, lengths = golfCourseBeans.length; j < lengths; j++) {
						list.add(golfCourseBeans[j]);
					}
					map.put(mark, list);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	public static List<GolfCourseBean> parseList(
			Map<String, List<GolfCourseBean>> result) {
		List<GolfCourseBean> list = new ArrayList<GolfCourseBean>();
		if (result != null && result.size() > 0) {
			Set<Map.Entry<String, List<GolfCourseBean>>> set = result
					.entrySet();
			for (Iterator<Map.Entry<String, List<GolfCourseBean>>> it = set
					.iterator(); it.hasNext();) {
				Map.Entry<String, List<GolfCourseBean>> entry = (Map.Entry<String, List<GolfCourseBean>>) it
						.next();
				list.addAll(entry.getValue());
			}
		}
		return list;
	}

	public static List<String> parsePriceList(
			Map<String, List<GolfCourseBean>> result) {
		List<String> list = new ArrayList<String>();
		if (result != null && result.size() > 0) {
			Set<Entry<String, List<GolfCourseBean>>> set = result.entrySet();
			for (Iterator<Map.Entry<String, List<GolfCourseBean>>> it = set
					.iterator(); it.hasNext();) {
				Entry<String, List<GolfCourseBean>> entry = (Map.Entry<String, List<GolfCourseBean>>) it
						.next();
				list.add(entry.getKey());
			}
		}
		Collections.sort(list, new SortString(Commons.SORT_UP));
		return list;
	}
}
